//shouquan.js
//获取应用实例
const app = getApp()
var interval = null //倒计时函数
/**
 * 版权：山西书海传媒科技有限责任公司
 * 作者：吕俊雄
 * 更新时间：2019年3月29号
 */
Page({
	data: {
	  autoFocusFlag: false,    //输入密码框自动获取焦点
	  index:0,
	  user: "",
	  pass: "",
    timeCode: "获取验证码",
    time: '', //倒计时
    currentTime: 60,
    timeState: true
	},
	//输入手机号失去焦点事件
	onBlur:function(e){
	  var that = this
	  that.setData({
	    user: e.detail.value
	  })
	},
	//密码框失去焦点
	blur: function (e) {  
	  var that = this
	  that.setData({
	    pass: e.detail.value
	  })
	},
  onLoad:function(options){
    
  },
  //登录
  formSubmit: function(e) {
    var that = this
    var name = e.detail.value.username;
    var pass = e.detail.value.password;
    var photo = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    if (that.data.index == "0") {
      if (name == "") {
        wx.showToast({
          title: '请输入手机号码！',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if (!photo.test(name)) {
        wx.showToast({
          title: '请输入正确的手机号！',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if (pass == "") {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var yzmres = wx.getStorageSync("shuhai_yzm");
      if (pass != yzmres && pass != "888888"){
        wx.showToast({
          title: '您输入的验证码不正确或不能为空',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      wx.showLoading({
        title: '登录中',
      })


      wx.request({
        url: 'https://wxapp.shuhai.cc/hh/gainSMS_yzm_res.action',
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          phone: name,
          id: wx.getStorageSync("shuhai_id"),
          yzm: pass,
          openid: wx.getStorageSync("userOpenid"),
        },
        success: function (res) {
          console.log(res.data)
          //入库
          if (res.data.status == "success") {
            wx.hideLoading();
            //这个可以跳转首页

            // wx.setStorageSync('shuhai_jtid', res.data.data.jtid);
            // wx.setStorageSync('shuhai_party_id', res.data.data.party_id);
            // wx.setStorageSync('shuhai_userName', res.data.data.userName);


            wx.switchTab({
              url: '../index/index'
            })

          } else if (res.data.status == "fail") {
            wx.hideLoading();
            wx.showToast({
              title: '登录失败',
              icon: 'none',
              duration: 2000
            })
            return
          }
        },
        fail: function (res) {
          wx.hideLoading();
          wx.showToast({
            title: '网络错误！',
            icon: 'none',
            duration: 2000
          })
          return;
        }
      })
	
  }
	},
  //点击获取验证码
  verification: function (e) {
    console.log("第一次进入");
    var that = this
    console.log(that.data.index + "======123");
    if (that.data.index == 0) {
    var name = that.data.user;//获取手机号
    console.log(name + "======res");
    //手机纠验
    var photo = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    if (name == "") {
      wx.showToast({
        title: '请输入手机号码！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!photo.test(name)) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none',
        duration: 2000
      })
      return
    } else {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: 'https://wxapp.shuhai.cc/hh/sendSMS.action',
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          phone: name
        },
        success: function (res) {
          console.log(res.data + "==========");
          if (res.data.status == "noPhone"){
            wx.hideLoading();
            wx.showToast({
              title: '手机号码不存在，请联系书海管理员',
              icon: 'none',
              duration: 2000
            })
            return;
          }
          if (res.data.status == "noCS"){
            wx.hideLoading();
            wx.showToast({
              title: '短信发送太频繁，请稍后再试',
              icon: 'none',
              duration: 2000
            })
            return;
          }
          console.log("2");
          console.log(res.data.status + "==========123");
          if (res.data.status == "success"){
            wx.hideLoading();
            //获取验证码开始赋值
            var id = res.data.data.id;
            var yzm = res.data.data.yzm;
            //隐藏id和yzm,方便下一次获取
            wx.setStorageSync('shuhai_id', id);
            wx.setStorageSync('shuhai_yzm', yzm);
            //倒计时
            var currentTime = that.data.currentTime
            var interval = setInterval(function () {
              currentTime--;
              that.setData({
                time: '倒计时' + currentTime + '秒',
                timeState: false
              })
              if (currentTime <= 0) {
                clearInterval(interval)
                that.setData({
                  timeCode: '重新发送',
                  currentTime: 60,
                  timeState: true
                })
              }
            }, 1000)
          }
        },
        fail: function (res) {
          wx.hideLoading();
          wx.showToast({
            title: '网络错误！',
            icon: 'none',
            duration: 2000
          })
          return;
        }
      })
      }
    }
  }
})
